package IT.DataAccess;

import IT.Entities.UsuarioMenu;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DatUsuarioMenu implements IDatUsuarioMenu{
    private UsuarioMenu oMenu;
    private List<UsuarioMenu> oLista;
    private BDConexion oBase;
    
    private Connection oConexion;
    private CallableStatement oCst;    
    private ResultSet oRst;     
    
    public List<UsuarioMenu> Listar() {
       oLista=new ArrayList<UsuarioMenu>();
         
       try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioMenuListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new UsuarioMenu(oRst.getInt("cod_mnu_in"), 
                        oRst.getString("nom_mnu_vc"),oRst.getString("des_mnu_vc"),
                        oRst.getInt("tip_mnu_in"), oRst.getString("Tipo"), 
                        oRst.getInt("orden_mnu_in")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
            oBase=null;
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public UsuarioMenu Ind(int nCodigo) {
        oMenu=new UsuarioMenu(); 
        
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioMenuInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oMenu.setCod_mnu_in(oRst.getInt("cod_mnu_in"));
                oMenu.setNom_mnu_vc(oRst.getString("nom_mnu_vc"));
                oMenu.setDes_mnu_vc(oRst.getString("des_mnu_vc"));
                oMenu.setTip_mnu_in(oRst.getInt("tip_mnu_in"));
                oMenu.setTipo(oRst.getString("Tipo"));
                oMenu.setOrden_mnu_in(oRst.getInt("orden_mnu_in"));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
            oBase=null;
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oMenu;
    }

    public List<UsuarioMenu> Ubi(int nPerfil) {
       oLista=new ArrayList<UsuarioMenu>();
         
       try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioMenuUbi(?)}");
            oCst.setInt(1, nPerfil);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new UsuarioMenu(oRst.getInt("cod_mnu_in"), 
                        oRst.getString("nom_mnu_vc"),oRst.getString("des_mnu_vc"),
                        oRst.getInt("tip_mnu_in"), oRst.getString("Tipo"), 
                        oRst.getInt("orden_mnu_in")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
            oBase=null;
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }
    
    public void Insertar(UsuarioMenu ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioMenuInsertar(?,?,?, ?)}");              
        
            oCst.setString(1, ent.getNom_mnu_vc());
            oCst.setString(2, ent.getDes_mnu_vc());
            oCst.setInt(3, ent.getTip_mnu_in());
            oCst.setInt(4, ent.getOrden_mnu_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
            oBase=null;
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(UsuarioMenu ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioMenuEditar(?,?,? ,?,?)}");  
            
            oCst.setInt(1, ent.getCod_mnu_in());            
            oCst.setString(2, ent.getNom_mnu_vc());
            oCst.setString(3, ent.getDes_mnu_vc());
            oCst.setInt(4, ent.getTip_mnu_in());
            oCst.setInt(5, ent.getOrden_mnu_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
            oBase=null;
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(int nCodigo) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_UsuarioMenuEliminar(?)}");              
            oCst.setInt(1, nCodigo);          
            oCst.execute();
            oCst.close();
            oConexion.close();
            oBase=null;
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
